package util

import (
	"gopkg.in/natefinch/lumberjack.v2"
	"io"
	"io/ioutil"
	"log"
)

var (
	Request     *log.Logger //request
	Persistence *log.Logger //persistence
	Node        *log.Logger //Node
	Dispatch    *log.Logger //dispatch
	Param       *log.Logger //param
	Trace       *log.Logger //0
	Debug       *log.Logger //2
	Info        *log.Logger //4
	Warn        *log.Logger //6
	Error       *log.Logger //8
)

func init() {
	Init(".", 8)
}

func Init(path string, level int) {
	var requestHandle, persistenceHandle, nodeHandle, dispatchHandle, paramHandle, traceHandle, debugHandle, infoHandle, warnHandle, errorHandle io.Writer

	requestHandle = &lumberjack.Logger{
		Filename:   path + "/request.log",
		MaxSize:    500,
		MaxBackups: 20,
		MaxAge:     7,
		Compress:   false,
	}

	persistenceHandle = &lumberjack.Logger{
		Filename:   path + "/persistence.log",
		MaxSize:    500,
		MaxBackups: 20,
		MaxAge:     7,
		Compress:   false,
	}

	nodeHandle = &lumberjack.Logger{
		Filename:   path + "/node.log",
		MaxSize:    500,
		MaxBackups: 20,
		MaxAge:     7,
		Compress:   false,
	}

	dispatchHandle = &lumberjack.Logger{
		Filename:   path + "/dispatch.log",
		MaxSize:    500,
		MaxBackups: 20,
		MaxAge:     7,
		Compress:   false,
	}

	paramHandle = &lumberjack.Logger{
		Filename:   path + "/param.log",
		MaxSize:    500,
		MaxBackups: 20,
		MaxAge:     7,
		Compress:   false,
	}

	if level <= 0 {
		traceHandle = &lumberjack.Logger{
			Filename:   path + "/trace.log",
			MaxSize:    500,
			MaxBackups: 20,
			MaxAge:     7,
			Compress:   false,
		}
	} else {
		traceHandle = ioutil.Discard
	}

	if level <= 2 {
		debugHandle = &lumberjack.Logger{
			Filename:   path + "/debug.log",
			MaxSize:    500,
			MaxBackups: 20,
			MaxAge:     7,
			Compress:   false,
		}
	} else {
		debugHandle = ioutil.Discard
	}

	if level <= 4 {
		infoHandle = &lumberjack.Logger{
			Filename:   path + "/info.log",
			MaxSize:    500,
			MaxBackups: 20,
			MaxAge:     7,
			Compress:   false,
		}
	} else {
		infoHandle = ioutil.Discard
	}

	if level <= 6 {
		warnHandle = &lumberjack.Logger{
			Filename:   path + "/warn.log",
			MaxSize:    500,
			MaxBackups: 20,
			MaxAge:     7,
			Compress:   false,
		}
	} else {
		warnHandle = ioutil.Discard
	}

	if level <= 8 {
		errorHandle = &lumberjack.Logger{
			Filename:   path + "/error.log",
			MaxSize:    500,
			MaxBackups: 20,
			MaxAge:     7,
			Compress:   false,
		}
	} else {
		errorHandle = ioutil.Discard
	}

	Request = log.New(requestHandle, "request ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
	Persistence = log.New(persistenceHandle, "persistence ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
	Node = log.New(nodeHandle, "node ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
	Dispatch = log.New(dispatchHandle, "dispatch ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
	Param = log.New(paramHandle, "param ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
	Trace = log.New(traceHandle, "trace ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
	Debug = log.New(debugHandle, "debug ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
	Info = log.New(infoHandle, "info ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
	Warn = log.New(warnHandle, "warn ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
	Error = log.New(errorHandle, "error ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
}
